<?php
class OrderManageController extends Controller{

	public function __construct($data) {
		parent::__construct($data);
		$this->_set_head_footer();
	}

	public function index()
	{
		$this->initPage();
		$this->assign('indexflag', 1);
		$this->display('order_index.html');
	}

	public function initPage()
	{
		require_once( "../system/Class_M_DAO.php" );
		$M_dao = new Class_M_DAO;
		$sql = "SELECT 0 as checked,
		`id`,
		`content`,
		`category`,
		`price`,
		`freeset`,
		`freeprice`,
		`comment`
		FROM
		`m_orderextraprice`
		where category=0";
		$this->sendlist = $M_dao->get_rows($sql);

		$sql = "SELECT  0 as checked,
		`id`,
		`content`,
		`category`,
		`price`,
		`freeset`,
		`freeprice`,
		`comment`
		FROM
		`m_orderextraprice`
		where category=1";
		$this->buyinglist = $M_dao->get_rows($sql);



		$this->assign('check_status1', empty($this->p['check_status1'])?'':'checked');
		$this->assign('check_status2', empty($this->p['check_status2'])?'':'checked');
		$this->assign('check_status3', empty($this->p['check_status3'])?'':'checked');
		$this->assign('check_status4', empty($this->p['check_status4'])?'':'checked');
		$this->assign('text_search', (empty($this->p["text_search"])?"":$this->p["text_search"]));
		$this->assign('text_time1', (empty($this->p["text_time1"])?"":$this->p["text_time1"]));
		$this->assign('text_time2', (empty($this->p["text_time2"])?"":$this->p["text_time2"]));

		$this->assign('check_way1', empty($this->p['check_way1'])?'':'checked');
		$this->assign('check_way2', empty($this->p['check_way2'])?'':'checked');

		foreach($this->buyinglist as &$row)
		{
			$row["checked"]=(!isset($this->p['check_buying'.$row["id"]])?0:1);

		}
		foreach($this->sendlist as &$row)
		{
			$row["checked"]=(!isset($this->p['check_send'.$row["id"]])?0:1);

		}

		$this->assign('sendlist', $this->sendlist);
		$this->assign('buyinglist', $this->buyinglist);


	}

	public function search($page=1){
		//require_once( "../system/Class_M_DAO.php" );

		$showNum=empty($this->p["showNum"])?5:(int)$this->p["showNum"];
		$this->initPage();
		$allcount=$this->getordercount();
		$orderlist=$this->getorderdata($page,$showNum);

		$this->showpage($page,$allcount,$showNum,$orderlist);
	}

	function changepage()
	{
		$this->initPage();
		$nowpage=empty($this->p["nowpage"])?1:(int)$this->p["nowpage"];
		$this->search($nowpage);

	}

	function showpage($inpage=1,$allcount=0,$showNum,$orderlist=null)
	{


			$pagecount=ceil($allcount/$showNum);

			$this->assign('showNum',$showNum);
			$this->assign('pagecount', $pagecount);
			$this->assign('nowpage', $inpage);
			$this->assign('orderlist', $orderlist);
			$this->display('order_index.html');
	}

	function updatesended()
	{
		require_once( "../system/Class_M_DAO.php" );
		try {

			$orderids=$this->p['orderids'];
			if($orderids!="")
			{
			$setstatus=$this->p['setstatus'];
			$line=explode("-",$orderids);
			$str_where=" 1=0 ";
				foreach ($line as $value)
				{
					$str_where.=" OR order_id=".$value;
				}
				$SQL="update d_order SET order_status='".$setstatus."' WHERE ".$str_where.";";
				$M_dao = new Class_M_DAO;
				if(!$M_dao->exec_sql($SQL))
				{
					throw new Exception("update order error");
				}
			}
			echo "cart ajax success";
		}
		catch (Exception $e)
		{
			echo $e;
		}
	}

	function getordercount()
	{
		$M_dao = new Class_M_DAO;
		//=================================$str_status
		$str_status="1=0".(empty($this->p['check_status1'])?"":" or a.`order_status`='1'")
		.(empty($this->p['check_status2'])?"":" or a.`order_status`='2'")
		.(empty($this->p['check_status3'])?"":" or a.`order_status`='3'")
		.(empty($this->p['check_status4'])?"":" or a.`order_status`='4'");
		if($str_status!="1=0")
		{
			$str_status=" and (".$str_status.") ";
		}
		else
		{$str_status="";}
		//=================================$str_way
		$str_way="1=0".(empty($this->p['check_way1'])?"":" or a.`order_way`='1'")
		.(empty($this->p['check_way2'])?"":" or a.`order_way`='2'");
		if($str_way!="1=0")
		{
			$str_way=" and (".$str_way.") ";
		}
		else
		{$str_way="";}
		//=================================$str_buying
		$str_buying="1=0";
		foreach($this->buyinglist as $row)
		{
			if($row["checked"]==1)
				$str_buying.=" or a.`order_pay_id`=".$row["id"];
		}

		if($str_buying!="1=0")
		{
			$str_buying=" and (".$str_buying.") ";
		}
		else
		{$str_buying="";}
		//=================================$str_send
		$str_send="1=0";
		foreach($this->sendlist as $row)
		{
			if($row["checked"]==1)
				$str_send.=" or a.`order_send_id`=".$row["id"];
		}

		if($str_send!="1=0")
		{
			$str_send=" and (".$str_send.") ";
		}
		else
		{$str_send="";}

		$text_search=mysql_real_escape_string(empty($this->p["text_search"])?"":$this->p["text_search"]);
		$sql = "select count(s.user_id) as cnt from (SELECT a.`user_id`
						FROM  d_order a
						left join  m_user b on a.user_id=b.id
						left join m_orderextraprice c on a.order_pay_id=c.id
						left join m_orderextraprice d on a.order_send_id=d.id
						left join d_order_goods e on a.order_id=e.order_id
				  where 1=1
				  ". $str_status
						  .$str_way
						  .$str_buying
						  .$str_send
						  .(trim($text_search)==""?"":
						  		" and ( a.order_number like '".$text_search."%'"
						  		." or b.login_id like '".$text_search."%'"
						  		." or concat(b.user_name1,b.user_name2) like '".$text_search."%'"
						  		." or e.goods_id like '".$text_search."%'"
						  		." or e.goods_name like '".$text_search."%'"
						  		.")")
						  		.(empty($this->p["text_time1"])?"":" and DATE_FORMAT(order_time,'%Y-%m-%d')>='".date("Y-m-d",strtotime($this->p["text_time1"]))."'")
						  		.(empty($this->p["text_time2"])?"":" and DATE_FORMAT(order_time,'%Y-%m-%d')<='".date("Y-m-d",strtotime($this->p["text_time2"]))."'")."
				  group by a.`order_id`,
				  a.`user_id`,
				  a.`address_name`,
				  a.`send_name1`,
				  a.`send_name2`,
				  a.`send_name_f1`,
				  a.`send_name_f2`,
				  a.`send_office_name`,
				  a.`send_office_name_f`,
				  a.`send_zipcode`,
				  a.`send_local_name`,
				  a.`send_treet_name`,
				  a.`send_address`,
				  a.`send_tel_number`,
				  a.`order_pay_id`,
				  a.`order_pay`,
				  a.`order_send_id`,
				  a.`order_send`,
				  a.`send_comment`,
				  a.`buying_price`,
				  a.`send_price`,
				  a.`tax`,
				  a.`order_number`,
				  a.`order_time`,
				  a.`order_way`,
				  a.`order_status`,
				  b.user_name1,
				  b.user_name2,
				  c.content,
				  d.content) as s
				";
		$ordercount = $M_dao->get_rows($sql);

		return $ordercount[0]['cnt'];

	}

	function getorderData($SQLpage=1,$SQLcount=5)
	{
		$M_dao = new Class_M_DAO;
		//=================================$str_status
		$str_status="1=0".(empty($this->p['check_status1'])?"":" or a.`order_status`='1'")
		.(empty($this->p['check_status2'])?"":" or a.`order_status`='2'")
		.(empty($this->p['check_status3'])?"":" or a.`order_status`='3'")
		.(empty($this->p['check_status4'])?"":" or a.`order_status`='4'");
		if($str_status!="1=0")
		{
			$str_status=" and (".$str_status.") ";
		}
		else
		{$str_status="";}
		//=================================$str_way
		$str_way="1=0".(empty($this->p['check_way1'])?"":" or a.`order_way`='1'")
		.(empty($this->p['check_way2'])?"":" or a.`order_way`='2'");
		if($str_way!="1=0")
		{
			$str_way=" and (".$str_way.") ";
		}
		else
		{$str_way="";}
		//=================================$str_buying
		$str_buying="1=0";
		foreach($this->buyinglist as $row)
		{
			if($row["checked"]==1)
				$str_buying.=" or a.`order_pay_id`=".$row["id"];
		}

		if($str_buying!="1=0")
		{
			$str_buying=" and (".$str_buying.") ";
		}
		else
		{$str_buying="";}
		//=================================$str_send
		$str_send="1=0";
		foreach($this->sendlist as $row)
		{
			if($row["checked"]==1)
				$str_send.=" or a.`order_send_id`=".$row["id"];
		}

		if($str_send!="1=0")
		{
			$str_send=" and (".$str_send.") ";
		}
		else
		{$str_send="";}

		$text_search=mysql_real_escape_string(empty($this->p["text_search"])?"":$this->p["text_search"]);
		$sql = "SELECT 0 as checked,a.`order_id`,a.`user_id`,
				  a.`address_name`,
				  a.`send_name1`,
				  a.`send_name2`,
				  a.`send_name_f1`,
				  a.`send_name_f2`,
				  a.`send_office_name`,
				  a.`send_office_name_f`,
				  a.`send_zipcode`,
				  a.`send_local_name`,
				  a.`send_treet_name`,
				  a.`send_address`,
				  a.`send_tel_number`,
				  a.`order_pay_id`,
				  a.`order_pay`,
				  a.`order_send_id`,
				  a.`order_send`,
				  a.`send_comment`,
				  a.`buying_price`,
				  a.`send_price`,
				  a.`tax`,
				  a.`order_number`,
				  a.`order_time`,
				  a.`order_way`,
				  a.`order_status`,
				  b.user_name1,
				  b.user_name2,
				  c.content as pay_content,
				  d.content as send_content
				  ,sum(e.goods_unitprice*e.goods_num) as sumprice
								FROM  d_order a
								left join  m_user b on a.user_id=b.id
								left join m_orderextraprice c on a.order_pay_id=c.id
								left join m_orderextraprice d on a.order_send_id=d.id
								left join d_order_goods e on a.order_id=e.order_id
				  where 1=1
				  ". $str_status
						  .$str_way
						  .$str_buying
						  .$str_send
						  .(trim($text_search)==""?"":
						  		" and ( a.order_number like '".$text_search."%'"
						  		." or b.login_id like '".$text_search."%'"
						  		." or concat(b.user_name1,b.user_name2) like '".$text_search."%'"
						  		." or e.goods_id like '".$text_search."%'"
						  		." or e.goods_name like '".$text_search."%'"
						  		.")")
						  		.(empty($this->p["text_time1"])?"":" and DATE_FORMAT(order_time,'%Y-%m-%d')>='".date("Y-m-d",strtotime($this->p["text_time1"]))."'")
						  		.(empty($this->p["text_time2"])?"":" and DATE_FORMAT(order_time,'%Y-%m-%d')<='".date("Y-m-d",strtotime($this->p["text_time2"]))."'")."
				  group by a.`order_id`,
				  a.`user_id`,
				  a.`address_name`,
				  a.`send_name1`,
				  a.`send_name2`,
				  a.`send_name_f1`,
				  a.`send_name_f2`,
				  a.`send_office_name`,
				  a.`send_office_name_f`,
				  a.`send_zipcode`,
				  a.`send_local_name`,
				  a.`send_treet_name`,
				  a.`send_address`,
				  a.`send_tel_number`,
				  a.`order_pay_id`,
				  a.`order_pay`,
				  a.`order_send_id`,
				  a.`order_send`,
				  a.`send_comment`,
				  a.`buying_price`,
				  a.`send_price`,
				  a.`tax`,
				  a.`order_number`,
				  a.`order_time`,
				  a.`order_way`,
				  a.`order_status`,
				  b.user_name1,
				  b.user_name2,
				  c.content,
				  d.content
				  Limit ".(($SQLpage-1)*$SQLcount).",".$SQLcount."
				";

		$orderlist = $M_dao->get_rows($sql);
		return $orderlist;
	}

}